package com.wml.sellPhone.service.impl;

import com.wml.sellPhone.dao.CollectionMapper;
import com.wml.sellPhone.pojo.PojoCollection;
import com.wml.sellPhone.pojo.User;
import com.wml.sellPhone.service.ICollectionService;
import com.wml.sellPhone.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 收藏服务实现类
 *
 * @author Hayes
 * @date 2018-06-27 19:18
 */
@Service
public class CollectionSeviceImpl implements ICollectionService {

    @Autowired
    CollectionMapper collectionMapper;

    @Override
    public boolean insert(HttpServletRequest request, Long phoneId) {
        PojoCollection collection = new PojoCollection();
        User user = (User) request.getSession().getAttribute(Constant.USER_INFO);
        collection.setUserid(user.getUserid());
        collection.setPhoneid(phoneId);
        return collectionMapper.insert(collection) > 0;
    }

    @Override
    public boolean delete(HttpServletRequest request, Long phoneId) {
        User user = (User) request.getSession().getAttribute(Constant.USER_INFO);
        return collectionMapper.deleteByUserIdAndPhoneId(user.getUserid(),phoneId) > 0;
    }

    @Override
    public List<PojoCollection> selectByUserId(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(Constant.USER_INFO);
        return collectionMapper.selectByUserId(user.getUserid());
    }
}
